﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufBase.h"
#include <QtWidgets/QLabel>
#include "GnufQtItem.h"

#include <QtWidgets/QSizeGrip>

namespace gnuf {
    class GNUF_BASE_EXPORT IQtResizingGripObserver : public gcmp::WeakReferenceable
    {
    public:
        virtual void onDraggedAndMoved(int distanceX, int distanceY) = 0;
    };
    class GNUF_BASE_EXPORT QtResizingGrip final : public QLabel
    {
        Q_OBJECT
    public:
        QtResizingGrip();
        // The resizing grip will float over the pParentWgt.
        QtResizingGrip(QWidget * pParentWgt);
        virtual ~QtResizingGrip();
        
        bool isFloating() const { return m_floating; }
        
        void setObserver(IQtResizingGripObserver * pObserver)
        { m_wpObserver = pObserver; }
        IQtResizingGripObserver * observer()
        { return (m_wpObserver.IsValid() ? m_wpObserver.Get() : nullptr); }
        const IQtResizingGripObserver * observer() const
        { return (m_wpObserver.IsValid() ? m_wpObserver.Get() : nullptr); }

		void refreshStyle(int w, int h);

    protected:
        virtual void mousePressEvent(QMouseEvent *) override;
        virtual void mouseReleaseEvent(QMouseEvent *) override;
        virtual void mouseMoveEvent(QMouseEvent *) override;
        virtual void enterEvent(QEvent *) override;
        virtual void leaveEvent(QEvent *) override;
        virtual bool eventFilter(QObject *, QEvent *) override;
    signals:
        void draggedAndMoved(int distanceX, int distanceY);
    private:
        void initialize();
		void installEventFilter();
		void resetPixmap(int w, int h);

        const bool m_floating;
        gcmp::WeakPtr<IQtResizingGripObserver> m_wpObserver;
        struct { bool enabled; QPoint globalPos; } m_dragAndMove;
    };
}
